import pygtk
pygtk.require('2.0')
import gtk
import os

from mokocard.gui.modaldialog import ModalDialog
from mokocard.util.gtkutil import getScrolledWidget

class MainPage(gtk.VBox):

    # instance variables
    counter = -1

    def disableButtons(self):
        self.btOk.set_sensitive(False)
        self.btKo.set_sensitive(False)
        self.btAnswer.set_sensitive(False)
        self.btStats.set_sensitive(False)
        self.btStart.set_sensitive(False)
        self.btDelete.set_sensitive(False)
        
    def disableAllNoStart(self):
        self.btOk.set_sensitive(False)
        self.btKo.set_sensitive(False)
        self.btAnswer.set_sensitive(False)
        self.btStats.set_sensitive(False)
        self.btDelete.set_sensitive(False)
        self.btStart.set_sensitive(True)
        # clean textview
        self.textviewAnswer.get_buffer().set_text("")
        self.textviewQuestion.get_buffer().set_text("")

    def enableAllNoStart(self):
        self.btOk.set_sensitive(True)
        self.btKo.set_sensitive(True)
        self.btAnswer.set_sensitive(True)
        self.btStats.set_sensitive(True)
        self.btDelete.set_sensitive(True)
        self.btStart.set_sensitive(False)

    # change row options button
    def options(self, widget, currentRow, newRow):
              
        for bt in self.optionRowObjects[currentRow]:
            if bt == None:
                break
            self.hButtonBox.remove(bt)

        for bt in self.optionRowObjects[newRow]:
            if bt == None:
                break
            self.hButtonBox.pack_start(bt, False, False, 0)

    def delete(self,widget):
        # delete record
        ret = self.sqlUtil.deleteCards(self.counter)
        modal = ModalDialog("Deleted " + str(ret) + " records")
        self.textviewAnswer.get_buffer().set_text("")
        self.next()

    def stats(self, widget):
        vTot = (self.sqlUtil.cards[self.counter].okAnswer + self.sqlUtil.cards[self.counter].koAnswer)
        print vTot
        vOk = self.sqlUtil.cards[self.counter].okAnswer
        # no answer equals ZeroDivisionError
        vpercent = 0
        if vTot != 0:
            vpercent =  (float(vOk) / float(vTot)) * 100
            
        print vpercent
        self.textviewAnswer.get_buffer().set_text("Current Iteration: " + str(self.sqlUtil.cards[self.counter].iteration) + "\nOk: " + str(self.sqlUtil.cards[self.counter].okAnswer) + " - Ko: " + str(self.sqlUtil.cards[self.counter].koAnswer) + "\n%OK: " + str(vpercent))
        
    def ok(self, widget):
        # change data in db
        self.sqlUtil.updateCard(self.counter, True)
        self.textviewAnswer.get_buffer().set_text("")
        self.next()
        
    def ko(self, widget):
        # change data in db
        self.sqlUtil.updateCard(self.counter, False)
        self.textviewAnswer.get_buffer().set_text("")
        self.next()
        
    def start(self, widget):
        self.enableAllNoStart()
        self.next()
        
    def showResult(self, widget):
        if self.counter != -1:
            self.textviewAnswer.get_buffer().set_text(self.sqlUtil.cards[self.counter].answer)
        
    def next(self):

        if len(self.sqlUtil.cards) == 0:
            modal = ModalDialog("No cards")
            self.textviewQuestion.get_buffer().set_text("")
            self.disableButtons()
            return
            
        self.counter = self.counter + 1
        
        if self.counter == len(self.sqlUtil.cards):
            # dialog
            modal = ModalDialog("End of Cards")
            self.textviewQuestion.get_buffer().set_text("")
            self.disableButtons()
        else:
            self.textviewQuestion.get_buffer().set_text(self.sqlUtil.cards[self.counter].question)

    def build(self, configData):
        
        # textviews
        self.textviewQuestion = gtk.TextView()
        self.textviewQuestion.show()
        self.textviewQuestion.set_justification(gtk.JUSTIFY_CENTER)
        #textviewQuestion.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("grey"));
        self.textviewQuestion.set_editable(False)
        self.textviewQuestion.set_sensitive(False)
        # shift 15 pixel above first line and next paragraph
        # I don't find a valid method for text vertical align
        self.textviewQuestion.set_pixels_above_lines(10)
        # wrap mode
        self.textviewQuestion.set_wrap_mode(gtk.WRAP_WORD)
        scrolledWidgetQuestion = getScrolledWidget()
        scrolledWidgetQuestion.add(self.textviewQuestion)
        scrolledWidgetQuestion.show()
        
        self.textviewAnswer = gtk.TextView()
        self.textviewAnswer.show()
        self.textviewAnswer.set_justification(gtk.JUSTIFY_CENTER)
        self.textviewAnswer.set_editable(False)
        self.textviewAnswer.set_sensitive(False)
        self.textviewAnswer.set_wrap_mode(gtk.WRAP_WORD)
        scrolledWidgetAnswer = getScrolledWidget()
        scrolledWidgetAnswer.add(self.textviewAnswer)
        scrolledWidgetAnswer.show()
        
        # answer button (it should be bigger
        self.btAnswer = gtk.Button("Result")
        self.btAnswer.connect("clicked", self.showResult)
        self.btAnswer.show()
        
        # buttons - first line
        self.btOk = gtk.Button("Ok")
        self.btOk.connect("clicked", self.ok)
        self.btOk.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btOk.show()
        self.btKo = gtk.Button("Ko")
        self.btKo.connect("clicked", self.ko)
        self.btKo.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btKo.show()
        self.btStart = gtk.Button("Start")
        self.btStart.connect("clicked", self.start)
        self.btStart.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btStart.show()
        self.btMore = gtk.Button("More")
        self.btMore.connect("clicked", self.options, 0, 1)
        self.btMore.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btMore.show()
        
        # buttons - second line
        self.btStats = gtk.Button("Stats")
        self.btStats.connect("clicked", self.stats)
        self.btStats.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btStats.show()
        self.btDelete = gtk.Button("Delete")
        self.btDelete.connect("clicked", self.delete)
        self.btDelete.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btDelete.show()
        self.btBack = gtk.Button("Back")
        self.btBack.connect("clicked", self.options, 1, 0)
        self.btBack.set_size_request(configData.getButtonCardsWidth(), configData.getButtonCardsHeight())
        self.btBack.show()

        # two dimensional array for option objects
        self.optionRowObjects = [[None,None,None,None],[None,None,None,None]]
        self.optionRowObjects[0][0] = self.btOk
        self.optionRowObjects[0][1] = self.btKo
        self.optionRowObjects[0][2] = self.btStart
        self.optionRowObjects[0][3] = self.btMore
        self.optionRowObjects[1][0] = self.btStats
        self.optionRowObjects[1][1] = self.btDelete
        self.optionRowObjects[1][2] = self.btBack
        self.optionRowObjects[1][3] = None
        
        # hButtonBox for buttons
        self.hButtonBox = gtk.HButtonBox()
        self.hButtonBox.set_layout(gtk.BUTTONBOX_SPREAD)
        
        for bt in self.optionRowObjects[0]:
            self.hButtonBox.pack_start(bt, False, False, 0)

        #self.hButtonBox.pack_start(self.btOk, False, False, 0)
        #self.hButtonBox.pack_start(self.btKo, False, False, 0)
        #self.hButtonBox.pack_start(self.btStart, False, False, 0)
        #self.hButtonBox.pack_start(self.btMore, False, False, 0)
        
        self.hButtonBox.show()
        
        self.pack_start(scrolledWidgetQuestion, True, True, 0)
        self.pack_start(scrolledWidgetAnswer, True, True, 0)
        self.pack_start(self.btAnswer, True, True, 0)
        self.pack_start(self.hButtonBox, False, False, 0)
        
    def __init__(self, configData, sqlUtil):

        self.sqlUtil = sqlUtil
        
        gtk.VBox.__init__(self, False, 5)
        
        self.build(configData)
        self.show()